package org.abadirou.cdebacker.flst;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.abadirou.cdebacker.flst.api.TypeChambre;
import org.abadirou.cdebacker.flst.api.dao.TypeChambreDao;
import org.abadirou.cdebacker.flst.entity.TypeChambreImpl;

import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class JPATypeChambreDao implements TypeChambreDao {

	@Inject
	EntityManager em;

	@Override
	public TypeChambre createType(String string) {
		return new TypeChambreImpl(string);
	}

	@Override
	public TypeChambreImpl get(Long id) {
		return em.find(TypeChambreImpl.class, id);
	}

	@Override
	public void saveEntry(TypeChambre type) {
		EntityTransaction tx = em.getTransaction();

		tx.begin();
		em.persist(type);
		tx.commit();
	}

	@Override
	public TypeChambre getByTypeName(String string) {
		Query query = em.createNamedQuery(TypeChambreImpl.TYPE_BY_NAME);
		return (TypeChambre) query.setParameter("type", string)
				.getSingleResult();
	}

}
